package com.ayun.controller;

import com.ayun.bean.User;
import com.ayun.exception.InvalidArgument;
import com.ayun.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.sql.Date;

@RequestMapping("/user")
@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping("/getUserInfo")
    public User getUserInfo(String username) {
        User user = userService.getUserInfo(username);
        user.setDateStr(user.getBirth().toString());

        System.out.println(user);

        return user;
    }

    @ResponseBody
    @RequestMapping("/updateUserInfo")
    public boolean updateUserInfo(@RequestBody User user) throws InvalidArgument {
        user.setBirth(Date.valueOf(user.getDateStr()));

        System.out.println(user);

        if(!userService.updateUserInfo(user)) {
            return userService.insertUserInfo(user);
        }

        return true;
    }
}
